package com.example.sys.entity.types;

import com.example.common.util.SpringContextUtil;
import com.example.common.util.UserUtil;
import com.example.sys.entity.User;
import com.example.sys.service.UserService;
import org.apache.ibatis.type.BaseTypeHandler;
import org.apache.ibatis.type.JdbcType;
import org.apache.ibatis.type.MappedJdbcTypes;
import org.apache.ibatis.type.MappedTypes;

import java.sql.CallableStatement;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

/**
 * 用户信息 Handler
 * @author ZhengDing
 * @date 2022/1/29 15:11
 */
@MappedJdbcTypes({JdbcType.INTEGER, JdbcType.BIGINT})
@MappedTypes({User.class})
public class UserInfoTypeHandler extends BaseTypeHandler<User> {
    @Override
    public void setNonNullParameter(PreparedStatement ps, int i, User userInfo, JdbcType jdbcType) throws SQLException {
        System.out.println(userInfo);
        ps.setInt(i, userInfo.getId());
    }
    
    @Override
    public User getNullableResult(ResultSet resultSet, String colName) throws SQLException {
        System.out.println(colName);
        Integer userId = resultSet.getInt(colName);
        User user = UserUtil.getUser(userId);
        return user;
    }
    
    @Override
    public User getNullableResult(ResultSet resultSet, int colIndex) throws SQLException {
        Integer userId = resultSet.getInt(colIndex);
        User user = UserUtil.getUser(userId);
        return user;
    }
    
    @Override
    public User getNullableResult(CallableStatement callableStatement, int i) throws SQLException {
        Integer userId = callableStatement.getInt(i);
        User user = UserUtil.getUser(userId);
        return user;
    }
}
